package com.inspur.his.controller;

import com.alibaba.fastjson.JSON;
import com.inspur.his.pojo.DeptExt;
import com.inspur.his.pojo.MakeRegisterOrder;
import com.inspur.his.pojo.RenShi;
import com.inspur.his.service.DeptService;
import com.inspur.his.service.MakeRegisterOrderService;
import com.inspur.his.service.RenShiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * 预约挂号
 */
@Controller
@RequestMapping("/makeRegister")
public class MakeRegisterControllrt {

    private int pageSize = 10;

    @Autowired
    RenShiService renShiService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private MakeRegisterOrderService makeRegisterOrderService;

    /**
     * 跳转预约挂号页面
     * @param model
     * @return
     */
    @RequestMapping("/toAddMakeRegister")
    public String toAddRegister(Model model) {
        List<DeptExt> deptList = deptService.deptList(null);
        model.addAttribute("deptList", deptList);
        return "/outDepart/makeRegisterAdd.jsp";
    }

    /**
     * 跳转预约挂号更新页面
     * @param model
     * @return
     */
    @RequestMapping("/toUpdateMakeRegister")
    public String toUpdateMakeRegister(Model model, Integer roId) {
        MakeRegisterOrder makeRegisterOrder = makeRegisterOrderService.getOneById(roId);
        List<DeptExt> deptList = deptService.deptList(null);
        model.addAttribute("deptList", deptList);
        model.addAttribute("makeRegisterOrder", makeRegisterOrder);
        return "/outDepart/makeRegisterUpdate.jsp";
    }

    /**
     * 查询医生
     * @param departId
     * @return
     */
    @RequestMapping("/getDoctersByDepartId")
    @ResponseBody
    public Object getDoctersByDepartId(String departId) {
        HashMap map = new HashMap();
        map.put("departId", departId);
        List<RenShi> docterList = renShiService.renShiInList(map);
        return JSON.toJSON(docterList);
    }

    /**
     * 添加预约挂号信息
     * @return
     */
    @RequestMapping("/addMakeRegister")
    public String addMakeRegister(MakeRegisterOrder makeRegisterOrder) {
        makeRegisterOrderService.addMakeRegister(makeRegisterOrder);
        return "redirect:/makeRegister/showMakeRegister?currPage=1";
    }

    /**
     * 更新
     * @param makeRegisterOrder
     * @return
     */
    @RequestMapping("/updateMakeRegister")
    public String updateMakeRegister(MakeRegisterOrder makeRegisterOrder) {
        makeRegisterOrderService.update(makeRegisterOrder);
        return "redirect:/makeRegister/showMakeRegister?currPage=1";
    }

    /**
     * 删除
     * @param roId
     * @return
     */
    @RequestMapping("/deleteMakeRegister")
    public String deleteMakeRegister(Integer roId) {
        makeRegisterOrderService.delete(roId);
        return "redirect:/makeRegister/showMakeRegister?currPage=1";
    }

    /**
     * 分页查询预约挂号
     * @param model
     * @param departName
     * @param doctorName
     * @param currPage
     * @return
     */
    @RequestMapping("/showMakeRegister")
    public String showMakeRegister(Model model, String departName, String doctorName, Integer roId, int currPage) {
        int total = makeRegisterOrderService.count(departName, doctorName, roId);
        List<MakeRegisterOrder> list = makeRegisterOrderService.showMakeRegister(departName, doctorName, roId, currPage, pageSize);
        int lastPage = 1;
        if (total>10) {
            lastPage = total % pageSize == 0 ? total/pageSize : total/pageSize + 1;
        }
        model.addAttribute("total", total);
        model.addAttribute("lastPage", lastPage);
        model.addAttribute("currPage", currPage);
        model.addAttribute("makeRegisters", list);
        return "/outDepart/makeRegisterShow.jsp";
    }
}
